﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class DictionaryHelper
    {
        // Methods
        public static Dictionary<string, T> AddValue<T>(this Dictionary<string, T> dict, string key, T value)
        {
            if (dict.ContainsKey(key))
            {
                dict[key] = value;
                return dict;
            }
            dict.Add(key, value);
            return dict;
        }

        public static Dictionary<string, T> AddValues<T>(this Dictionary<string, T> dict, string[] keys, T[] values)
        {
            int index = 0;
            foreach (string str in keys)
            {
                dict.AddValue<T>(str, (index >= values.Length) ? default(T) : values[index]);
                index++;
            }
            return dict;
        }

        public static T GetValue<T>(this Dictionary<string, T> dict, string key)
        {
            return dict.GetValue<T>(key, default(T));
        }

        public static T GetValue<T>(this Dictionary<string, T> dict, string key, T defaultValue)
        {
            if (dict == null)
            {
                return default(T);
            }
            if (!dict.ContainsKey(key))
            {
                return defaultValue;
            }
            return dict[key];
        }
    }

}
